package com.cc.wechat.mq.consumer;

import com.cc.wechat.mq.constant.RabbitMQConstants;
import com.cc.wechat.websocket.handler.WebSocketHandler;
import com.cc.wechat.websocket.message.WebSocketMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 消息消费者
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MessageConsumer {

    private final WebSocketHandler webSocketHandler;

    /**
     * 处理私聊消息
     */
    @RabbitListener(queues = RabbitMQConstants.PRIVATE_MESSAGE_QUEUE)// 监听私聊消息队列
    public void handlePrivateMessage(WebSocketMessage<?> message) {
        log.info("收到私聊消息：{}", message);
        // 获取接收者ID
        Long receiverId = message.getReceiverId();
        // 通过WebSocket发送消息给接收者
        webSocketHandler.sendMessage(receiverId, message);
    }

    /**
     * 处理群聊消息
     */
    @RabbitListener(queues = RabbitMQConstants.GROUP_MESSAGE_QUEUE)// 监听群聊消息队列
    public void handleGroupMessage(WebSocketMessage<?> message) {
        log.info("收到群聊消息：{}", message);
        // TODO: 获取群成员列表并发送消息
    }

    /**
     * 处理好友请求
     */
    @RabbitListener(queues = RabbitMQConstants.FRIEND_REQUEST_QUEUE)// 监听好友请求队列
    public void handleFriendRequest(WebSocketMessage<?> message) {
        log.info("收到好友请求：{}", message);
        // 获取接收者ID
        Long receiverId = message.getReceiverId();
        // 通过WebSocket发送消息给接收者
        webSocketHandler.sendMessage(receiverId, message);
    }

    /**
     * 处理好友响应
     */
    @RabbitListener(queues = RabbitMQConstants.FRIEND_RESPONSE_QUEUE)// 监听好友响应队列
    public void handleFriendResponse(WebSocketMessage<?> message) {
        log.info("收到好友响应：{}", message);
        // 获取接收者ID
        Long receiverId = message.getReceiverId();
        // 通过WebSocket发送消息给接收者
        webSocketHandler.sendMessage(receiverId, message);
    }
} 